﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using Bll;
using System.Data;

namespace Accounting.admin.Professional
{
    public partial class ProfessionalRevise : System.Web.UI.Page
    {
        ModelPublic m = new ModelPublic();
        BllProfessional b = new BllProfessional();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (Session["Name"] != null && Session["Type"] != null)
                {

                    txtAuthor.Text = Session["Name"].ToString();
                    txtDateTime.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");

                    UserType.Text = Session["Type"].ToString();

                    if (UserType.Text == "0")
                    {
                        Recommend.Visible = true;
                    }
                    else
                    {
                        Recommend.Visible = false;
                    }
                    if (Request.QueryString["id"] != null)
                    {
                        m.Id = Request.QueryString["id"].ToString();
                        lblId.Text = Request.QueryString["id"].ToString();

                        SelectBind();
                        ContentBind();
                    }
                }
                else
                {
                    btnReturn.Enabled = false;
                    ASubmit.Disabled = false;
                }
            }
        }

        private void SelectBind()
        {
            m.NavId = "3";
            DataTable dt = b.SubNav_Inquire_All(m);
            foreach (DataRow my in dt.Rows)
            {
                string name = "-" + my["Name"].ToString() + "-";
                string id = my["Id"].ToString();
                NavSubList.Items.Add(new ListItem(name, id));
            }
            DataTable nav = b.Nav_Inquire_All();

            foreach (DataRow my in nav.Rows)
            {
                if (my["Id"].ToString() == "3")
                {
                    H2_Title.InnerText = "修改：" + my["Name"].ToString();
                }
            }
        }

        private void ContentBind()
        {
            m.Id = lblId.Text;
            DataTable dt = b.Professional_Inquire_Single(m);

            foreach (DataRow my in dt.Rows)
            {
                txtAuthor.Text = my["Author"].ToString();
                Content.Text = my["_Content"].ToString();
                txtDateTime.Text = my["DateTime"].ToString();
                txtNumber.Text = my["Number"].ToString();
                txtTitle.Text = my["Title"].ToString();
                NavSubList.SelectedValue = my["NavSubId"].ToString();
                if (my["Recommend"].ToString() == "True")
                    Recommend.Checked = true;
                else
                    Recommend.Checked = false;
            }
        }

        protected void btnRevise_Click(object sender, EventArgs e)
        {
            m.Id = lblId.Text;
            m.Title = txtTitle.Text.Trim();
            m.Author = txtAuthor.Text.Trim();
            m.DateTime = txtDateTime.Text.Trim();
            m.Content = Content.Text;
            m.NavId = "3";
            m.NavSubId = NavSubList.SelectedValue;
            if (txtNumber.Text == "")
                m.Number = "0";
            else
                m.Number = txtNumber.Text.Trim();

            if (UserType.Text == "0")
            {
                if (Recommend.Checked)
                    m.Recommend = "True";
                else
                    m.Recommend = "False";
            }
            else
            {
                m.Recommend = "false";
            }


            string message = b.Professional_Modify(m);
            if (message == "1")
            {
                Response.Write("<script>alert('修改成功！')</script>");


                if (Recommend.Checked)
                    Response.Redirect("./ProfessionalAdmin.aspx");
                else
                    Response.Redirect("./ProfessionalUnpublished.aspx");
            }
            else
            {
                Response.Write("<script>alert('" + message + "')</script>");
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["type"] != null)
            {
                if (Request.QueryString["type"].ToString() == "0")
                    Response.Redirect("./ProfessionalAdmin.aspx");
                else
                    Response.Redirect("./ProfessionalUnpublished.aspx");
            }
        }

        private void ClearContext()
        {
            txtAuthor.Text = "";
            txtDateTime.Text = "";
            txtNumber.Text = "";
            txtTitle.Text = "";
            Content.Text = "";
        }
    }
}